package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(string, Department.class);

        //查询MongoDB是否有科室信息
        Department departmentExist = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        //有，做更新操作
        //判断
        if (departmentExist != null) {
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setId(departmentExist.getId());
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

        //没有，做添加操作

    }

    @Override
    public Page findDepartmentPage(Map<String, Object> request) {
        String hoscode = (String) request.get("hoscode");
        Integer page = Integer.parseInt((String) request.get("page"));
        Integer limit = Integer.parseInt((String) request.get("limit"));

        Department department = new Department();
        department.setHoscode(hoscode);
        Sort createTime = Sort.by(Sort.Direction.ASC, "createTime");

        Example example = Example.of(department);
        PageRequest pageRequest = PageRequest.of(page-1, limit, createTime);

        return departmentRepository.findAll(example, pageRequest);

    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null){
            departmentRepository.deleteById(department.getId());
        }

    }

    @Override
    public List<DepartmentVo> getAllDepts(String hoscode) {
        //1.根据医院编号查询所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);

        //2.组装前端所需的数据
        //key：大科室编号，value：当前大科室下的所有子科室列表
        Map<String, List<Department>> map = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        List<DepartmentVo> bigDeptList = new ArrayList<>();

        for (Map.Entry<String, List<Department>> entry : map.entrySet()) {
            DepartmentVo departmentVo = new DepartmentVo();
            String bigcode = entry.getKey();
            departmentVo.setDepcode(bigcode);

            List<Department> smallDeptList = entry.getValue();

            departmentVo.setDepname(smallDeptList.get(0).getBigname());

            List<DepartmentVo> children = new ArrayList<>();


            for (Department department1 : smallDeptList) {
                DepartmentVo small = new DepartmentVo();
                String depcode = department1.getDepcode();
                String depname = department1.getDepname();
                small.setDepname(depname);
                small.setDepcode(depcode);

                children.add(small);
            }
            departmentVo.setChildren(children);
            bigDeptList.add(departmentVo);

        }

        return bigDeptList;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
    }
}
